var BarcodeEnum = require('../../../Barcode/server/controllers/Enum.server.controller'),	
	userController = require('../../../User/server/controllers/users.server.controller'),
	barcodeMasterController = require('../../../Barcode/server/controllers/BarcodeMaster.server.controller');

//根据条码主档获取员工与角色信息
exports.getUserRoles = function(req, res) {
	var barcode = req.body.Parameter.Barcode;
	barcodeMasterController.findBarcodeMaster(barcode, function(err, barcodeMaster){
		if(err){
        	res.json({
                Data: null,
                Error: err
            });
        }
        else{
        	if(barcodeMaster == null){
        		var newErr = new Error();
                newErr.leval = 9;
                newErr.title = '查询条码主档失败';
                newErr.message = '该条码没有对应的条码主档！';
                res.json({
	                Data: null,
	                Error: newErr
	            });
        	}
        	else{
        		if(barcodeMaster.BarcodeType != BarcodeEnum.BarcodeType.Person){
        			var newErr = new Error();
	                newErr.leval = 9;
	                newErr.title = '查询员工条码主档失败';
	                newErr.message = '该条码对应的不是员工类型的条码主档！';
	                res.json({
		                Data: null,
		                Error: newErr
		            }); 
        		}
        		else{
        			userController.getUserAndRolesInfo(barcodeMaster.EntityID, function(err, userReocrd){
        				if(err){
				        	res.json({
				                Data: null,
				                Error: err
				            });
				        }
				        else{
				        	var isQC = false;
				        	for(var i=0; i<userReocrd.Roles.length; i++){
				        		if(userReocrd.Roles[i].Code == "QC"){
				        			isQC = true;
						            break;
				        		}
				        	}

				        	userReocrd.IsQC = isQC;
				        	delete userReocrd.Roles;
				        	res.json({
				                Data: userReocrd,
				                Error: null
				            });
				        }
        			});
        		}
        	}
        }
	});
};